<?
require("../../common/init.php");
include(ROOT."common/class_image_resize.php");
require(ROOT."common/image_zoom.php");
require("author.php");
require("upfile.php");

$product_pic=new DB(Tproduct_pic,$conn);
$gee=$product_pic; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象 gee=role object
$cate=$product; // cate = category, 分类

$cateid=isset($_GET["cid"])?clean($_GET["cid"]):0;
$url='product_pic';
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$id=isset($_GET["id"])?clean($_GET["id"]):0;

$product_name=$product->get_name($cateid,$lang);
$title=$product_name."-".ROOT_PICTURE_GALLERY;

// sql action / sql 动作
switch ($action){
	case "update":
		sql_update();
		break;
	case "add":
		sql_insert();
		break;
}

if($id==0){
	$where=array();
	$where[]=array('name'=>'product_id','val'=>$cateid);
	$info=$gee->get_rows(32,'product_id',$cateid);
	$total_pics=$gee->count_rows($where);
}
else $info=$gee->get_row($id);

function add()
{
	global $url,$h;
	$row = array();
	echo $h->form($url.'.php', 1, '');
	show_box($row, true, true);		
	echo $h->hidden('action', 'add');
	echo $h->former();
}
function delete($id)
{
	global $gee,$url,$cateid;
	$upload_folder=ROOT.'upload/';
	$info=$gee->get_row($id);
	$old_pic_small=$upload_folder.$info['small_pic'];
	$old_pic_large=$upload_folder.$info['large_pic'];
	if($gee->delete($id)){
		@unlink($old_pic_small);
		@unlink($old_pic_large);
		wheel($url.'.php?cid='.$cateid);
	}
}
function show_info($info)
{
	global $gee,$id;
	if($id==0) show_boxes($info);
	else show_box($info,false,false);
}
function sql_insert()
{
	global $gee,$url,$arrlang;
	$upload_folder=ROOT.'upload/';
	$cateid=iso(@$_POST["cid"]);

	for($i=0;$i<21;$i++){
		$cfile='cpic'.$i;
		if(isset($_FILES[$cfile]) && $_FILES[$cfile]['size']!=0 ){
			$pic=upfile($_FILES[$cfile]);
			$large_pic=substr($pic,0,strlen($pic)-4).'-large.jpg';
			//$image_resize=new Image_resize($upload_folder.$pic,PRODUCT_LARGE_PIC_WIDTH,PRODUCT_LARGE_PIC_HEIGHT,0,$upload_folder.$large_pic);
			image_zoom($upload_folder.$pic, $upload_folder.$large_pic,PRODUCT_LARGE_PIC_WIDTH,PRODUCT_LARGE_PIC_HEIGHT,'#ffffff');
			//$image_resize=new Image_resize($upload_folder.$pic,PRODUCT_SMALL_PIC_WIDTH,PRODUCT_SMALL_PIC_HEIGHT,1,$upload_folder.$pic);
			image_zoom($upload_folder.$pic, $upload_folder.$pic,PRODUCT_SMALL_PIC_WIDTH,PRODUCT_SMALL_PIC_HEIGHT,'#ffffff');
			$arr=array(
				'product_id'=>$cateid,
				'small_pic'=>$pic,
				'large_pic'=>$large_pic,
				);
			$gee->insert($arr);
		} // add to cpic
	}// for
	// if($gee->insert($arr))
	//exit;
	wheel($url.'.php?cid='.$cateid);
}

// view 加载视图
require ROOTVIEW.$url.'.php';
